using System;
using System.Collections;

using LocalBoard.Domain;
using LocalBoard.Persistence;

namespace LocalBoard.Controller
{
	/// <summary>
	/// Summary description for AdminController.
	/// </summary>
	public class AdminController
	{
		private IAccessorFactory _accessorFactory;
		private IUserRoleAccessor _userRoleAccessor;
		private IProfileAccessor _profileAccessor;
		private IClubAccessor _clubAccessor;
		private IClubCategoryAccessor _clubCategoryAccessor;

		public AdminController(IAccessorFactory accessorFactory)
		{
			if(accessorFactory == null)
				throw new ArgumentNullException("accessorFactory");
			_accessorFactory = accessorFactory;
			_userRoleAccessor = _accessorFactory.GetUserRoleAccessor();
			_profileAccessor = _accessorFactory.GetProfileAccessor();
			_clubAccessor = _accessorFactory.GetClubAccessor();
			_clubCategoryAccessor = _accessorFactory.GetClubCategoryAccessor();
		}

		public int CreateUser(Profile profile)
		{
			return _profileAccessor.Insert(profile);
		}

		public Profile GetUser(int profileId)
		{
			return _profileAccessor.Get(profileId);
		}

		public Profile GetUser(string userId)
		{
			return _profileAccessor.GetProfileByUserId(userId);
		}

		public UserRole GetUserRole(int roleId)
		{
			return _userRoleAccessor.Get(roleId);
		}

		public UserRole GetUserRole(string roleName)
		{
			Hashtable roles = _userRoleAccessor.GetAllUserRoles();
			foreach(int roleId in roles.Keys)
			{
				UserRole role = roles[roleId] as UserRole;
				if(role.Name == roleName)
				return role;
			}
			return null;
		}

		public int CreateUserRole(string roleName)
		{
			return _userRoleAccessor.Insert(roleName);
		}

		public void AssignUserRoleToUser(int roleId, int profileId)
		{
			_userRoleAccessor.AddRoleToProfile(roleId, profileId);
		}

		public int CreateClub(Club club)
		{
			return _clubAccessor.Insert(club);
		}

		public Club GetClub(int clubId)
		{
			return _clubAccessor.Get(clubId);
		}

		public Club GetClub(string clubUniqueName)
		{
			foreach(Club club in _clubAccessor.GetClubList())
			{
				if(club.UniqueName.ToLower() == clubUniqueName.ToLower())
					return club;
			}
			return null;
		}

		public Profile GetAnonymousUser()
		{
			return _profileAccessor.GetAnonymousUser();
		}

		public Profile GetUserByUserIdAndPassword(string userId, string password)
		{
			return _profileAccessor.GetProfileByUserIdAndPassword(userId, password);
		}

		public ArrayList GetUserRolesAssignedToUser(int profileId)
		{
			return _userRoleAccessor.GetRolesByProfileId(profileId);
		}

		public void UnAssignRoleFromUser(int roleId, int profileId)
		{
			_userRoleAccessor.DeleteRoleFromProfile(roleId, profileId);
		}

		public ArrayList GetAllClubList()
		{
			return _clubAccessor.GetClubList();
		}

		public ArrayList SearchClub(string keyword)
		{
			return _clubAccessor.GetClubListByKeyword(keyword);
		}

		public void UpdateClub(Club club)
		{
			_clubAccessor.Update(club);
		}

		public void DeleteClub(int clubId)
		{
			_clubAccessor.Delete(clubId);
		}

		public void UpdateUser(Profile profile)
		{
			_profileAccessor.Update(profile);
		}

		public Hashtable GetAllClubCategories()
		{
			return _clubCategoryAccessor.GetAllClubCategories();
		}

		public int CreateClubCategory(string clubCategoryName)
		{
			return _clubCategoryAccessor.Insert(clubCategoryName);
		}

		public void DeleteClubCategory(int clubCategoryId)
		{
			_clubCategoryAccessor.Delete(clubCategoryId);
		}

		public void UpdateClubCategory(string clubCategoryName, int clubCategoryId)
		{
			_clubCategoryAccessor.Update(clubCategoryName, clubCategoryId);
		}

		public ArrayList GetAllUserList()
		{
			return _profileAccessor.GetAllUserProfiles();
		}

		public Hashtable GetAllUserRoles()
		{
			return _userRoleAccessor.GetAllUserRoles();
		}
	}
}
